<template>
  <ex-dialog width="1000px" class="ex-diolog" title="图片广告（最多可添加10个）" :visible="visible" @closeDialog="handleClose">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="180px" class="demo-ruleForm">
      <div>
        
        <div class="bannerBox" v-for="(item, index) in ruleForm.advertiseList" :key="200 + index">
          <div class="imgBox">
            <el-form-item :label="`上传图片背景${index+1}`" :prop="`advertiseList.${index}.img`" :rules="rules.img" ref="imgRef">
              <ImageUploadMore :limit="1" :fileSize="20" @successImg="(e) => { getBannerImg(e, index) }"
                :value="item.img">
              </ImageUploadMore>
            </el-form-item>
            <el-form-item label="跳转链接:" :prop="`advertiseList.${index}.type`" :rules="rules.type">
              <el-radio-group v-model="item.type">
                <el-radio label="1">无跳转</el-radio>
                <!-- <el-radio label="2">其他H5</el-radio> -->
              </el-radio-group>
            </el-form-item>
          </div>
          
          <div class="rig_btn_box">
            <el-button type="primary" circle icon="el-icon-arrow-up" @click="upGo(index)" v-if="index != 0"></el-button>
            <el-button type="primary" circle icon="el-icon-arrow-down" @click="downGo(index)"
              v-if="index != ruleForm.advertiseList.length - 1"></el-button>
            <el-button type="danger" icon="el-icon-delete" circle @click="handleDelete(index)"></el-button>
          </div>
        </div>
      </div>
      <div class="lable_l">
        <el-button type="primary" @click="handleAdd">新增</el-button>
      </div>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')" :loading="loading">确认</el-button>
        <el-button @click="handleClose('ruleForm')">取消</el-button>
      </el-form-item>
    </el-form>
  </ex-dialog>
</template>
<script>
// components
import Loading from '@/components/Loading/index'
// api
import {
  advertise,
  advertiseList,
} from '@/api/incentiveManagement/dyConfig'

export default {
  components: {
    Loading
  },
  props: {
    visible: Boolean,
    id: String,
    pathType: String,
  },
  data() {

    return {
      title: '新增轮播图',
      switchModel: '1',//开启轮播图或者背景图 1 轮播图 2 背景图
      activeName: '1', //1 轮播图 2 背景图
      bjId: '',//背景图Id
      loading: false,
      imgLoading: false,
      ruleForm: {
        advertiseList: [
          {
            img: '',//图片url
            type: '1',//跳转链接类型（1 无跳转）
          },
        ],
      },
      rules: {
        type: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
        img: [
          { required: true, message: '请上传图片', trigger: 'change' }
        ],
      }
    }
  },
  computed: {

  },
  created() {
    this.getDatail()
  },
  methods: {
    handleClick(tab, event) {
      console.log(tab, event);
    },
   
    //获取字典值
    getadminDict() {
      const params = {
        dictType: "app_redirect_type",
      };
      adminDict(params).then((res) => {
        this.appTypeList = res.result[0].dictList
      });
    },

    
   
    //获取轮播图详情
    getDatail() {
      advertiseList().then(res => {
        this.ruleForm.advertiseList = res.result
      })
    },
    /** logo */
    getImg(e) {
      this.ruleForm.img = e
      this.$refs['imgRef'].clearValidate();
    },
    //新建
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          var data = {
            ...this.ruleForm
          }
          advertise(data).then(res => {
            setTimeout(() => {
              this.$message.success('修改成功')
              this.handleClose()
              this.$emit('success')
              this.loading = false
            }, 1000);
          }).catch(() => {
            this.loading = false
          }).finally(() => {

          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

    //取消
    handleClose(formName) {
      this.$emit('update:visible', false)
      // this.$refs[formName].resetFields();
    },
    //确认
    handleConfirm() {
      this.$emit('success', '你好')
    },
    handleSuccess(msg) {
      if (msg === '已拒绝') {
        var type = 'error'
      } else {
        var type = 'success'
      }
      this.$message({
        message: msg,
        type: type,
        duration: 1500,
        onClose: () => {
          this.handleClose()
          this.$emit('success')
        }
      })
    },
    //新增轮播图
    handleAdd() {
      if(this.ruleForm.advertiseList.length < 10){
        var obj = {
          img: '',//图片url
          type: '1',//跳转链接类型（1 无跳转）
        }
        this.ruleForm.advertiseList.push(obj)
      }else{
        this.$message.error('最多添加10条');
        
      }
      
    },
    //删除轮播图
    handleDelete(ind) {
      this.ruleForm.advertiseList.splice(ind, 1)
    },
    //轮播图广告上传图片
    getBannerImg(e, index) {
      this.ruleForm.advertiseList[index].img = e
    },
    //上传图片
    getImg(e) {
      this.ruleForm.img = e
      this.$refs['imgRef'].clearValidate();
    },
    //上移一位
    upGo(index) {
      // console.log('index', index)
      var arr = this.ruleForm.advertiseList
      if (index != 0) {
        // console.log(arr.splice(index - 1, 1, arr[index][0]))
        this.ruleForm.advertiseList[index] = arr.splice(index - 1, 1, arr[index])[0]
        // console.log('arr', arr)
      } else {
        this.ruleForm.advertiseList.push(arr.shift())
      }
    },
    //下移一位
    downGo(index) {
      var arr = this.ruleForm.advertiseList
      if (index != arr.length - 1) {
        this.ruleForm.advertiseList[index] = arr.splice(index + 1, 1, arr[index])[0]
      } else {
        this.ruleForm.advertiseList.unshift(arr.splice(index, 1)[0])
      }
    }

  }
}
</script>
<style lang="scss" scoped>
.ex-diolog {
  ::v-deep .ex--dialog--footer {
    border: none !important;
    padding: 0 !important;
  }

  ::v-deep .ex--dialog--main {
    padding: 10px 20px !important;
  }
}

::v-deep .avatar-uploader2 .el-upload {
  width: 100% !important;
}

.avatar-uploader2 {
  width: 130px;
  height: 130px;
  text-align: center;
  line-height: 130px;
  background-color: #fbfdff;
  border: 1px dashed #c0ccda;
}

.banner_img {
  width: 130px;
  height: 130px;
}

.el-input {
  width: 300px;
}

.lable_l {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px 0 20px 0;

  .left {
    margin-right: 20px;
  }
}

.bannerBox {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;

  .rig_btn_box {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    width: 100px;
    height: 200px;

    .el-button {
      margin: 0;
    }
  }
}

.imgBox {
  border: 1px solid #f5f5f5;
  border-radius: 12px;
  padding: 20px;
  width: 800px;

  .name {
    font-size: 16px;
    color: #606266;
    margin-bottom: 10px;
    padding-left: 50px;
  }
}
</style>